<?php
require_once(dirname(__FILE__)."/../../config.php");
require_once(dirname(__FILE__)."/HostDao.php");
require_once(dirname(__FILE__).'/../init.php');
?>
<?php
/**
* This is ServerListType Dao class
* @package inc/class/server
* @author Roger Wang
*/
class WFServerListTypeDao {
	/**
	 * WFDatabaseObject object
	 * @var object
	 */
	private $_objhostdb;
	/**
	 * ServerListType table name
	 * @var string
	 */
	private $_strhdbtable;

	/**
	 * Error string
	 * @var string
	 */
	private $_strerrMessage = "";
	/**
	 * Error code
	 * @var int
	 */
	private $_interrCode = 0;
	
	/**
	 *  Run SQL query
	 * @param string $strsqlstring,array $arrsqlvalues,string $strestring,int $intecode
	 * @return query_result
	 */
	private function _runSQL($strsqlstring, $arrsqlvalues, $strestring, $intecode) {
		$this->resetErrorInfo();
		$arrquery_result = $this->_objhostdb->query($strsqlstring, $arrsqlvalues);
		// check error
		if( $this->_objhostdb->isFailed() ) {
			$this->_strerrMessage =  $strestring.": ".$this->_objhostdb->getErrMessage();
			$this->_interrCode = $intecode;
			return FALSE;
		}
		return $arrquery_result;
	}
    private function setError($strErrMessage,$intErrCode){
        $this->_strerrMessage =  $strErrMessage.": ".WF::getDataObject()->getErrMessage();
		$this->_interrCode = $intErrCode;
		return FALSE;
    }
	/**
	 *  Constructor
	 * @param array $arrsdbconfig
	 * @return None
	 */
	public function __construct($arrsdbconfig) {
		$this->resetErrorInfo();
		$this->_strhdbtable = $arrsdbconfig["table"]["serverlisttype"]["name"];
		//$this->_objhostdb = new WFDatabaseObject($arrsdbconfig);
		$this->_objhostdb = WF::getDataObject();
		// check error
		//william su - DataObject is singleton, so if check error will cause error
		/*if( $this->_objhostdb->isFailed() ) {
			$this->_strerrMessage =  "Database initialization failed: ".$this->_objhostdb->getErrMessage();
			$this->_interrCode = 1;
		}*/
	}
	/**
	 * Destructor
	 * @param None
	 * @return None
	 */
	public function __destruct() {
		unset ( $this->_objhostdb );
	}
	
	/**
	 * Add Record
	 * @param string $strtype, int $passwordonly
	 * @return query_result
	 */
	public function add($strtype, $intpasswordonly, $jsoncustomfields='') {
		/*$strsqlstring = 'INSERT into ' . $this->_strhdbtable . ' (type, passwordonly, customfields) VALUES (:type,:passwordonly,:customfields)';
		$arrsqlvalues [':type'] = $strtype;
		$arrsqlvalues [':passwordonly'] = $intpasswordonly;
        $arrsqlvalues [':customfields'] = $jsoncustomfields;
		return $this->_runSQL ( $strsqlstring, $arrsqlvalues, "Adding server list type failed", 1 );*/
	    $objParameter = WF::getParameter();
	    $objParameter->appendFrom($this->_strhdbtable);
	    $assocData = array();
	    $assocData['type'] = $strtype;
	    $assocData['passwordonly'] = $intpasswordonly;
	    $assocData['customfields'] = $jsoncustomfields;
	    $objParameter->setAssocData($assocData);
	    $result = WF::getDataObject()->queryInsertByParameter($objParameter);
	    if(false === $result){
	        $strErrMessage = "Adding server list type failed";
	        $intErrCode = 1;
	        $this->setError($strErrMessage, $intErrCode);
	    }
	    return $result;
	}

	/**
	 * Delete Record
	 * @param int $intrid
	 * @return query_result
	 */
	public function del( $intrid ) {
		/*$strsqlstring = 'DELETE from '.$this->_strhdbtable.' WHERE id = :rid';
		$arrsqlvalues[':rid'] = $intrid;
		return $this->_runSQL( $strsqlstring, $arrsqlvalues, "Deleting server list type failed", 2);*/
	    $objParameter = WF::getParameter();
	    $objParameter->appendFrom($this->_strhdbtable);
	    $objParameter->andWhereEqual('id',$intrid);
	    $result = WF::getDataObject()->queryDeleteByParameter($objParameter);
	    if(false === $result){
	        $strErrMessage = "Deleting server list type failed";
	        $intErrCode = 2;
	        $this->setError($strErrMessage, $intErrCode);
	    }
	    return $result;
	}

	/**
	 * Update Record
	 * @param int $intrid
	 * @return query_result
	 */
	public function update( $intrid, $strtype, $intpasswordonly, $jsoncustomfields='') {
		/*$strsqlstring = 'UPDATE '.$this->_strhdbtable.' SET type = :type, passwordonly = :passwordonly, customfields = :customfields WHERE id = :rid';
		$arrsqlvalues[':type'] = $strtype;
		$arrsqlvalues[':passwordonly'] = $intpasswordonly;
		$arrsqlvalues[':rid'] = $intrid;
        $arrsqlvalues[':customfields'] = $jsoncustomfields;
		return $this->_runSQL( $strsqlstring, $arrsqlvalues, "Updating server list type failed", 3);*/
	    $objParameter = WF::getParameter();
	    $objParameter->appendFrom($this->_strhdbtable);
	    $assocData = array();
	    $assocData['type'] = $strtype;
	    $assocData['passwordonly'] = $intpasswordonly;
	    $assocData['customfields'] = $jsoncustomfields;
	    $objParameter->setAssocData($assocData);
	    $objParameter->andWhereEqual('id',$intrid);
	    $result = WF::getDataObject()->queryUpdateByParameter($objParameter);
	    if(false === $result){
	        $strErrMessage = "Updating server list type failed";
	        $intErrCode = 3;
	        $this->setError($strErrMessage, $intErrCode);
	    }
	    return $result;
	}

	/**
	 * Get all records with no filter
	 * @param NONE
	 * @return query_result
	 */
	public function getall_nofilter() {
		/*$arrresult_array = array();
		$strsqlstring = 'SELECT * from '.$this->_strhdbtable;
		$arrsqlvalues = array();
		$arrresult_array = $this->_runSQL( $strsqlstring, $arrsqlvalues, "Getting server list type failed", 5);
		if( $this->isFailed() ) {
			return FALSE;
		}*/
	    $objParameter = WF::getParameter();
	    $objParameter->appendFrom($this->_strhdbtable);
	    $arrresult_array = WF::getDataObject()->querySelectAllByParameter($objParameter);
	    if(false === $arrresult_array){
	        $strErrMessage = "Getting server list type failed";
	        $intErrCode = 5;
	        $this->setError($strErrMessage, $intErrCode);
	        return FALSE;
	    }
		return $arrresult_array;
	}

	/**
	 * Get all records
	 * @param NONE
	 * @return query_result
	 */
	public function getall() {
		$arrresult_array = $this->getall_nofilter();
		return $arrresult_array;
	}
	/**	
	 * Get last record ID
	 * @param NONE
	 * @return last record ID
	 */
	public function getlastid() {
		return $this->_objhostdb->lastInsertId();
	}

	/**
	 *  Check failed or not
	 * @param NONE
	 * @return failed or not
	 */
	public function isFailed() {
		if( $this->_interrCode != 0 || $this->_strerrMessage != "" ) {
			return TRUE;
		}
		return FALSE;
	}
	/**
	 *  Get Error message
	 * @param NONE
	 * @return error message
	 */
	public function getErrMessage() {
		return $this->_strerrMessage;
	}
	/**
	 *  Get Error code
	 * @param NONE
	 * @return error code
	 */
	public function getErrCode() {
		return $this->_interrCode;
	}
	
	private function resetErrorInfo() {
		$this->_strerrMessage = "";
		$this->_interrCode = 0;
	}
}
